package cn.iocoder.yudao.module.mybusiness.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mybusiness.domain.BussinessMod;
import cn.iocoder.yudao.module.mybusiness.domain.Facility;
import cn.iocoder.yudao.module.mybusiness.domain.ProductionRecord;
import cn.iocoder.yudao.module.mybusiness.domain.Task;
import cn.iocoder.yudao.module.mybusiness.mapper.FacilityMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.ModMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.RecordMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.TaskMapper;
import cn.iocoder.yudao.module.mybusiness.param.RecordParam;
import cn.iocoder.yudao.module.mybusiness.service.RecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, ProductionRecord> implements RecordService {


    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private FacilityMapper facilityMapper;

    @Override
    public PageResult<ProductionRecord> getList(RecordParam recordParam) {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(1);
        pageParam.setPageSize(Integer.MAX_VALUE);
        PageResult<ProductionRecord> recordPageResult = recordMapper.selectPage(recordParam, new LambdaQueryWrapperX<ProductionRecord>().orderByDesc(ProductionRecord::getId));
        PageResult<Task> taskPageResult = taskMapper.selectPage(pageParam, new LambdaQueryWrapperX<Task>());
        PageResult<Facility> facilityPageResult = facilityMapper.selectPage(pageParam, new LambdaQueryWrapperX<Facility>());
        Map taskMap = new HashMap();
        Map facility = new HashMap();
        Map craftMap = new HashMap();
        if(taskPageResult.getTotal()>0){
            taskMap = taskPageResult.getList().stream().collect(Collectors.toMap(Task::getId, Task::getName));
        }
        if(facilityPageResult.getTotal()>0){
            facility = facilityPageResult.getList().stream().collect(Collectors.toMap(Facility::getId, Facility::getFacName));
            craftMap = facilityPageResult.getList().stream().collect(Collectors.toMap(Facility::getId, Facility::getCraft));
        }
        if(recordPageResult != null && recordPageResult.getTotal()>0){
            Map finalTaskMap = taskMap;
            Map finalFacility = facility;
            Map finalCraftMap = craftMap;
            recordPageResult.getList().forEach(productionRecord -> {
                productionRecord.setTaskName((String)finalTaskMap.get(productionRecord.getTaskId()));
                productionRecord.setFacName((String) finalFacility.get(productionRecord.getFacId()));
                productionRecord.setCraft((String) finalCraftMap.get(productionRecord.getFacId()));
            });
        }
        return recordPageResult;
    }
}
